#include"declare.h"

//主菜单
void Main_menu()
{
	printf("**********************************************************\n");
	printf("**\t\t市大学生运动会分数统计系统\t\t**\n");
	printf("**********************************************************\n");
	printf("**\t\t1.录入信息\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t2.查询信息\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t3.修改信息\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t4.退出\t\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**********************************************************\n");
}

//三级菜单-排名信息
void Rank_menu()
{
	printf("******************************************\n");
	printf("**\t\t排名信息查询\t\t**\n");
	printf("******************************************\n");
	printf("**\t\t1.男子团体\t\t**\n");
	printf("**\t\t\t\t\t**\n");
	printf("**\t\t2.女子团体\t\t**\n");
	printf("**\t\t\t\t\t**\n");
	printf("**\t\t3.学校总分\t\t**\n");
	printf("**\t\t\t\t\t**\n");
	printf("**\t\t4.退出\t\t\t**\n");
	printf("**\t\t\t\t\t**\n");
	printf("******************************************\n");
}

//二级菜单-录入信息
void Input_menu()
{
	printf("**********************************************************\n");
	printf("**\t\t市大学生运动会分数统计系统\t\t**\n");
	printf("**********************************************************\n");
	printf("**\t\t1.录入学校信息\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t2.录入体育项目信息\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t3.录入体育项目比赛获奖信息\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t4.返回主菜单\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**********************************************************\n");	
} 

//二级菜单-查询信息
void Check_menu()
{
	printf("**********************************************************\n");
	printf("**\t\t市大学生运动会分数统计系统\t\t**\n");
	printf("**********************************************************\n");
	printf("**\t\t1.查询学校信息\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t2.查询所有体育项目信息\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t3.查询学校项目的情况\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t4.查询体育项目的比赛结果\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t5.查询排名信息\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t6.最终获奖情况\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t7.返回主菜单\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**********************************************************\n");
}

//二级菜单-修改信息 
void Review_menu() 
{
	printf("**********************************************************\n");
	printf("**\t\t市大学生运动会分数统计系统\t\t**\n");
	printf("**********************************************************\n");
	printf("**\t\t1.修改学校信息\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t2.修改体育项目信息\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**\t\t3.返回主菜单\t\t\t\t**\n");
	printf("**\t\t\t\t\t\t\t**\n");
	printf("**********************************************************\n");
}

//学校链表初始化
School *Initlist_school()
{
	School *first = (School*)malloc(sizeof(School));
	first->next = NULL;
	return first;
}

//体育项目链表初始化
Sport *Initlist_sport()
{
	Sport *first = (Sport*)malloc(sizeof(Sport));
	first->next = NULL;
	return first;
}

//录入学校信息 
void Input_school(School *first)
{
	char ID[10];
	char name[20];
	int male_num;
	int female_num;
	School *p = first;
	
	printf("请输入学校ID：\n");
	scanf("%s", ID);
	
	//判断录入的学校是否已存在 
	while(p !=NULL && strcmp(p->ID, ID) != 0)
	{
		p = p->next;
	}
	if(p != NULL)
	{
		printf("该学校已存在。\n");
		system("pause");
		return;
	}
	
	printf("请输入学校名字：\n");
	scanf("%s", name);
	printf("请输入参赛男生人数：\n");
	scanf("%d", &male_num);
	printf("请输入参赛女生人数：\n");
	scanf("%d", &female_num);
	
	p = first;
	//申请节点s 
	School *s = NULL;
	s = (School*)malloc(sizeof(School)); 
	strcpy(s->ID, ID);
	strcpy(s->name, name);
	s->male_num = male_num;
	s->male_score = 0;
	s->female_num = female_num;
	s->female_score = 0;
	s->all_score = 0;
	
	//将结点插入链表尾部
	while(p->next != NULL)
	{
		p = p->next;
	}
	s->next = p->next;
	p->next = s;
	//给出录入成功的提示 
	printf("学校信息录入成功！\n");
	system("pause"); 
	
	
	 
}

//录入体育项目信息
void Input_sport(Sport *first)
{
	int ID;
	char name[20];
	int score[5];
	char sex[2];
	Sport *p = first->next;
	
	printf("请输入项目ID：\n");
	scanf("%d", &ID);
	
	//判断录入的项目是否已存在 
	while(p !=NULL && p->ID != ID)
	{
		p = p->next;
	}
	if(p != NULL)
	{
		printf("该体育项目已存在。\n");
		system("pause");
		return;
	}
	
	printf("请输入项目名：\n");
	scanf("%s", name);
	while(1)
	{
		printf("请输入组别(男/女)：\n");
		scanf("%s", sex);
		if(strcmp(sex, "男") == 0|| strcmp(sex, "女") == 0)
		{
			break;
		}
		else
		{
			printf("非法输入，请重新输入。\n");
		}
	}
	
	
	//申请结点s用于存储信息
	Sport *s = (Sport*)malloc(sizeof(Sport));
	s->ID = ID;
	strcpy(s->school[0], "无");
	strcpy(s->name, name);
	strcpy(s->sex, sex);
	
	//根据ID的奇偶性来判断该项目是前三名还是前五名有分
	if(ID%2 != 0)  
	{
		s->score[0] = 7;
		s->score[1] = 5;
		s->score[2] = 3;
		s->score[3] = 2;
		s->score[4] = 1;
		
	}
	else
	{
		s->score[0] = 5;
		s->score[1] = 3;
		s->score[2] = 2;
		s->score[3] = 0;
		s->score[4] = 0;
	}
	
	//将结点按编号顺序插入 
	p = first; 
	while(p->next != NULL && p->next->ID < ID)
	{
		p = p->next;
	}
	s->next = p->next;
	p->next = s;
	//给出录入成功提示
	printf("体育项目信息录入成功！\n");
	system("pause"); 
}

//录入获奖信息
void Input_rank(Sport *first_sport, School *first_shool)
{
	int ID;
	printf("请输入需要录入比赛结果的体育项目编号：\n");
	scanf("%d", &ID);
	
	//找到符合条件的结点
	Sport *p = first_sport->next;
	while(p != NULL && p->ID != ID)
	{
		p = p->next;
	}	
	if(p == NULL)
	{
		printf("输入编号有误，请重新输入。\n");
		system("pause");
		return;
	}
	
	if(strcmp(p->school[0], "无") != 0)	//判断该项目是否录入过信息  
	{
		printf("该项目获奖信息已录入，请勿重复录入。\n");
		system("pause");
		return;
	}
	char school[20];
	if(ID % 2 == 0)
	{
		for(int i = 0; i < 3; i++)
		{
			printf("请输入%s第%d名：\n", p->name, i+1);
			scanf("%s", school);
			School *q = first_shool->next;
			while(q != NULL && strcmp(q->name, school) != 0)
			{
				q = q->next;
			}
			if(q == NULL)
			{
				i--;
				printf("该学校不存在，请重新输入。\n");
				continue;
			}
			strcpy(p->school[i], school); 
		}
	}
	else
	{
		for(int i = 0; i < 5; i++)
		{
			printf("请输入%s第%d名：\n", p->name, i+1);
			scanf("%s", school);
			School *q = first_shool->next;
			while(q != NULL && strcmp(q->name, school) != 0)
			{
				q = q->next;
			}
			if(q == NULL)
			{
				i--;
				printf("该学校不存在，请重新输入。\n");
				continue;
			}
			strcpy(p->school[i], school);  
		}
	}
	printf("获奖信息录入成功。\n");
	system("pause"); 
}

//保存学校信息
void Save_school(School *first)
{
	FILE *fp = fopen("D:/SportRecord/school.txt", "wb");
	if(fp == NULL)  //判断文件是否存在
	{
		printf("未找到学校信息文件。\n");
		return;
	}
	
	School *p = first->next;
	while(p != NULL)  //将信息写入文件
	{
		fwrite(p, sizeof(School), 1, fp);
		p = p->next;
	}
	fclose(fp);
}

//保存体育项目信息
void Save_sport(Sport *first)
{
	FILE *fp = fopen("D:/SportRecord/sport.txt", "wb");
	if(fp == NULL)  //判断文件是否存在
	{
		printf("未找到体育项目信息文件。\n");
		return;
	}
	
	Sport *p = first->next;
	while(p != NULL)  //将信息写入文件
	{
		fwrite(p, sizeof(Sport), 1, fp);
		p = p->next;
	}
	fclose(fp);
}

//读取学校信息
void Read_school(School *first)
{
	FILE *fp = fopen("D:/SportRecord/school.txt", "rb");
	if(fp == NULL)  //判断文件是否存在
	{
		printf("未找到学校信息文件。\n");
		return;
	}
	
	School *p = first;
	School temp;  //临时存储从文件中读取的信息
	School *s = NULL;
	while(fread(&temp, sizeof(School), 1, fp) == 1)
	{
		s = (School*)malloc(sizeof(School));
		*s = temp;
		s->next = p->next;
		p->next = s;
		p = p->next; 
	}
	fclose(fp);
	
} 

//读取体育项目信息
void Read_sport(Sport *first)
{
	FILE *fp = fopen("D:/SportRecord/sport.txt", "rb");
	if(fp == NULL)	//判断文件是否存在
	{
		printf("未找到体育项目信息文件。\n");
		return;
	}
	
	Sport *p = first;
	Sport temp;  //临时存储从文件中读取的信息
	Sport *s = NULL;
	while(fread(&temp, sizeof(Sport), 1, fp) == 1)
	{
		s = (Sport*)malloc(sizeof(Sport));
		*s = temp;
		s->next = p->next;
		p->next = s;
		p = p->next;
	}
	fclose(fp);
}

//修改学校信息
void Revise_shcool(School *first)
{
	char ID[10];
	printf("请输入需修改学校信息的学校ID：\n");
	scanf("%s", ID);
	system("cls");
	
	//查找符合条件的结点
	School *p = first->next;
	while(p != NULL && strcmp(p->ID, ID) != 0)
	{
		p = p->next;
	}
	if(p == NULL)
	{
		printf("输入ID有误，请重新输入。\n");
		system("pause");
		system("cls");
		return;
	}
	
	//给出提示，根据用户选择，来修改相应信息
	char choice[2];
	int revise;
	
	while(1)
	{
		printf("您有以下选择（学校：%s）：\n", p->name);
		printf("1.修改参赛男生人数  2.修改参赛女生人数  3.退出\n");
		printf("请输入您的选择：");
		scanf("%s", choice);
		system("cls");
		if(strcmp(choice, "1") == 0)
		{
			printf("请输入修改后的参赛男生人数：");
			scanf("%d", &revise);
			p->male_num = revise;
			printf("修改成功！\n");
		}
		else if(strcmp(choice, "2") == 0)
		{
			printf("请输入修改后的参赛女生人数：");
			scanf("%d", &revise);
			p->female_num = revise;
			printf("修改成功！\n");
		}
		else if(strcmp(choice, "3") == 0)
		{
			break;
		}
		else
		{
			printf("非法输入，请重新输入。\n");
		}
		system("pause");
		system("cls");
	}
	
	
}

//修改体育项目信息
void Revise_sport(Sport *first)
{
	int ID;
	char choice[2];
	printf("请输入需修改体育项目信息的编号：\n");
	scanf("%d", &ID);
	system("cls");
	
	//查找符合条件的结点
	Sport *p = first->next;
	while(p != NULL && p->ID!= ID)
	{
		p = p->next;
	}
	if(p == NULL)
	{
		printf("输入ID有误，请重新输入。\n");
		system("pause");
		system("cls");
		return;
	}
	
	while(1)
	{
		//给出提示，根据用户选择，来修改相应信息
		printf("您有以下选择：\n");
		printf("1.修改获奖情况    2.退出\n");
		printf("请输入你的选择；\n");
		scanf("%s", choice);
		system("cls");
	
		if(strcmp(choice, "1") == 0)
		{
			int select = 0;
			char name[20];
			if(p->ID % 2 != 0)
			{
				printf("1. 修改第一名获奖学校   2. 修改第二名获奖学校   3. 修改第三名获奖学校   4. 修改第四名获奖学校   5. 修改第五名获奖学校\n"); 
				printf("请输入你的选择：\n");
				scanf("%d", &select);
				system("cls");
				printf("请输入修改后的获奖学校：\n");
				scanf("%s", name);
				strcpy(p->school[select-1], name);
			}
			else
			{
				printf("1. 修改第一名获奖学校   2. 修改第二名获奖学校   3. 修改第三名获奖学校\n");
				printf("请输入你的选择：\n");
				scanf("%d", &select);
				system("cls");
				printf("请输入修改后的获奖学校：\n");
				scanf("%s", name);
				strcpy(p->school[select-1], name);
			}
			printf("修改成功！\n");
		}
		else if(strcmp(choice, "2") == 0) 
		{
			break;
		}
		else
		{
			printf("输入有误，请重新输入。\n");
		}
		system("pause");
		system("cls");
}
}

//查询学校信息
int Get_school(School *first)
{
	//根据用户输入的学校编号，查询该学校的基本信息 
	char ID[10];
	printf("请输入需要查询的学校编号：\n");
	scanf("%s", ID);
	system("cls");
	
	//查找对应的节点
	School *p = first->next;
	while(p != NULL && strcmp(ID, p->ID) != 0)
	{
		p = p->next;	
	}
	if(p == NULL)  //未找到相关学校 
	{
		printf("输入ID有误，未找到相关学校的信息。\n");
		return 0; 
	}
	else      //找到相关学校 
	{
		printf("**************************************************************************************************************************************************************************\n");
    	printf("**\t学校编号\t**\t\t校名\t\t**\t男生参赛人数\t**\t女生参赛人数\t**\t男生团体得分\t**\t女生团体得分\t**\t总得分\t**\n");
    	printf("**************************************************************************************************************************************************************************\n");
    	printf("**\t%-10s\t**\t%-20s\t**\t%10d\t**\t%10d\t**\t%10d\t**\t%10d\t**\t%5d\t**\n", p->ID, p->name, p->male_num, p->female_num, p->male_score, p->female_score, p->all_score);
    	printf("**************************************************************************************************************************************************************************\n");
		return 1;
	}
	
}

//查询所有体育项目信息
void Get_sport(Sport *first)
{
	Sport *p = first->next;
	
	printf("******************************************************************************************************************\n");
	printf("**\t体育项目编号\t**\t体育项目名\t\t**\t组别(男/女)\t**\t前五名或前三名成绩\t**\n");
	printf("******************************************************************************************************************\n");
	while(p != NULL)
	{
		if(p->score[3] == 0)
		{
			printf("**\t%d\t\t**\t%s\t\t**\t%s\t\t**\t%d %d %d\t\t\t**\n", p->ID, p->name, p->sex, p->score[0], p->score[1], p->score[2]);
		}
		else
		{
			printf("**\t%d\t\t**\t%s\t\t**\t%s\t\t**\t%d %d %d %d %d\t\t**\n", p->ID, p->name, p->sex, p->score[0], p->score[1], p->score[2], p->score[3], p->score[4]);
		}
		p = p->next;
		
	}
}

//查询学校项目的情况
void Get_SchoolProjectData(School *first_school, Sport *first_sport)
{
	char ID[10];
	School *p = first_school->next;
	Sport *q = first_sport->next;
	
	printf("请输入学校ID：\n");
	scanf("%s", ID);
	system("cls");
	
	while(p != NULL && strcmp(p->ID, ID) != 0)	//寻找符合条件的结点
	{
		p = p->next;
	}
	if(p == NULL)	//未找到符合条件的结点
	{
		printf("输入有误，请重新输入。\n");
		return;
	}
	
	printf("******************************************\n");
	printf("**\t%s比赛情况\t\t**\n", p->name);
	printf("******************************************\n");
	printf("**\t项目\t\t**\t排名\t**\n");
	printf("******************************************\n");
	
	while(q != NULL)	//以此获取所有结点信息
	{
		for(int i = 0; i < 5; i++)
		{
			if(strcmp(p->name, q->school[i]) == 0)
			{
				printf("**\t%s\t**\t第%d名\t**\n", q->name, i+1);
			}	
		}
		q = q->next;
	}
	printf("******************************************\n");
	
}

//查询体育项目的比赛结果
void Get_SportsResult(Sport *first)
{
	int ID;
	int limit = 5;
	printf("请输入体育项目ID：\n");
	scanf("%d", &ID);
	system("cls");
	
	Sport *p = first->next;
	while(p != NULL && p->ID != ID)	//获取符合条件的结点信息
	{
		p = p->next;
	}
	if(p == NULL)	//未找到
	{
		printf("输入有误，请重新输入。\n");
		return;
	}
	
	if(strcmp(p->school[0], "无") == 0)		//判断该项目比赛结果出来没有
	{
		printf("%s项目比赛未结束, 没有获奖信息。\n", p->name);
		return;
	}
	
	printf("******************************************\n");
	printf("**\t\t%s\t\t**\n", p->name);
	printf("******************************************\n");
	printf("**\t排名\t**\t学校\t\t**\n");
	printf("******************************************\n");
	if(p->ID % 2 == 0) //根据项目成绩组成，设定循环次数限制
	{
		limit = 3;
	}
	for(int i = 0; i < limit; i++)
	{
		printf("**\t第%d名\t**\t%-10s\t**\n", i+1, p->school[i]);
	}
	printf("******************************************\n");
}

//最终获奖情况 
void Get_AwardDetails(School *first)
{
	School *p = NULL;
	Male_allscore(first);
	Female_allscore(first);
	Allscore(first);
	
	printf("**************************************************************************\n");
	printf("**\t学校编号\t**\t学校\t\t**\t荣誉称号\t**\n");
	printf("**************************************************************************\n");
	
	int i = 0;
	char value[3][3] = {"一", "二", "三"};
	p = first->next;
	while(i < 3)
	{
		printf("**\t%-10s\t**\t%-15s\t**\t团体%s等奖\t**\n", p->ID, p->name, value[i]);
		printf("**\t\t\t**\t\t\t**\t\t\t**\n");
		if(p->next != NULL && (p->all_score != p->next->all_score))
    	{
    		i++;
		}
		p = p->next;
	}
	
	i = 0;
	p = first->next;
	while(i < 3)
	{
		printf("**\t%-10s\t**\t%-15s\t**\t男子组%s等奖\t**\n", p->ID, p->name, value[i]);
		printf("**\t\t\t**\t\t\t**\t\t\t**\n");
		if(p->next != NULL && (p->male_score != p->next->male_score))
    	{
    		i++;
		}
		p = p->next;
	}
	
	i = 0;
	p = first->next;
	while(i < 3)
	{
		printf("**\t%-10s\t**\t%-15s\t**\t女子组%s等奖\t**\n", p->ID, p->name, value[i]);
		printf("**\t\t\t**\t\t\t**\t\t\t**\n");
		if(p->next != NULL && (p->female_score != p->next->female_score))
    	{
    		i++;
		}
		p = p->next;
	}
	
	p = first->next;
	while(p != NULL)
	{
		if(p->male_num + p->female_num >= 30)
		{
			printf("**\t%-10s\t**\t%-15s\t**\t优秀组织奖\t**\n", p->ID, p->name);
			printf("**\t\t\t**\t\t\t**\t\t\t**\n");
		}
		p = p->next;
	}
	printf("**************************************************************************\n");
}

//排名信息查询
void getRankInfo(School *first)
{
	char select[5];
	School *p = NULL;
	
	while(1)	//二级菜单选择
	{
		Rank_menu();
		printf("请输入您的选择：\n");
		scanf("%s", select);
		system("cls");
		if(strcmp(select, "1") == 0)	//按男子组总分排序
		{
			Male_allscore(first);
			p = first->next;
			printf("**************************************************************************************************************************\n");
    		printf("**\t名次\t**\t学校编号\t**\t\t校名\t\t**\t男生参赛人数\t**\t男生组总得分\t**\n");
    		printf("**************************************************************************************************************************\n");
    		int i = 1;
			while(p != NULL)
    		{
    			printf("**\t第%2d名\t**\t%-10s\t**\t%-20s\t**\t%10d\t**\t%10d\t**\n", i, p->ID, p->name, p->male_num, p->male_score);
    			if(p->next != NULL && (p->male_score != p->next->male_score))
    			{
    				i++;
				}
    			p = p->next;
			}
			printf("**************************************************************************************************************************\n");
			system("pause");
			system("cls");
		}
		else if(strcmp(select, "2") == 0)	//按女子组总分排序
		{
			Female_allscore(first);
			p = first->next;
			printf("**************************************************************************************************************************\n");
    		printf("**\t名次\t**\t学校编号\t**\t\t校名\t\t**\t女生参赛人数\t**\t女生组总得分\t**\n");
    		printf("**************************************************************************************************************************\n");
    		int i = 1;
			while(p != NULL)
    		{
    			printf("**\t第%2d名\t**\t%-10s\t**\t%-20s\t**\t%10d\t**\t%10d\t**\n", i, p->ID, p->name, p->female_num, p->female_score);
    			if(p->next != NULL && (p->female_score != p->next->female_score))
    			{
    				i++;
				}
    			p = p->next;
			}
			printf("**************************************************************************************************************************\n");
			system("pause");
			system("cls");
		}
		else if(strcmp(select, "3") == 0)	//按团体总分排序
		{
			Allscore(first);
			p = first->next;
			printf("**************************************************************************************************************************\n");
    		printf("**\t名次\t**\t学校编号\t**\t\t校名\t\t**\t参赛总人数\t**\t团体得分\t**\n");
    		printf("**************************************************************************************************************************\n");
    		int i = 1;
			while(p != NULL)
    		{
    			printf("**\t第%2d名\t**\t%-10s\t**\t%-20s\t**\t%10d\t**\t%10d\t**\n", i, p->ID, p->name, p->male_num + p->female_num, p->all_score);
    			if(p->next != NULL && (p->all_score != p->next->all_score))
    			{
    				i++;
				}
    			p = p->next;
			}
			printf("**************************************************************************************************************************\n");
			system("pause");
			system("cls");
		}
		else if(strcmp(select, "4") == 0)	//退出
		{
			break;
		}
		else
		{
			printf("输入有误，请重新输入。\n");
		}
	}
}

//计算总分
void Score(School *first_school, Sport *first_sport)
{
	School *p = first_school->next;
	Sport *q = first_sport->next;
	int limit = 5;	
	
	while(q != NULL)	//获取所有结点信息
	{
			if(q->ID % 2 == 0) //根据项目成绩组成，设定循环次数限制
			{
				limit = 3;
			}
			
			for(int i = 0; i < limit; i++)
			{
				while(strcmp(p->name, q->school[i]) != 0)	//找到符合条件的结点
				{
					p = p->next;
				}
				
				p->all_score += q->score[i];	//学校总分
				if(strcmp(q->sex, "男") == 0)
				{
					p->male_score += q->score[i];	//男子团体总分
				}
				else
				{
					p->female_score += q->score[i];	//女子团体总分
				}
				
				p = first_school->next;	//p指针指回头结点				
			}
			q = q->next;
	}	
}

//按男子组总分排序
void Male_allscore(School *first)
{
	// 已排序部分初始化为原链表中第一个节点开始（头节点除外）
    School *sorted = first->next;
    // 头节点的下一个节点先设为NULL，与后面结点断开
    first->next = NULL;

    while (sorted!= NULL)
	{
        // 记录当前结点
        School *current = sorted;
        // 记录下个结点，为下一次循环提供条件。
        sorted = sorted->next;

        // 从头结点开始，从已排序的部分寻找插入位置
        School *prev = first;
        // 记录头节点下一个结点的位置
        School *next = first->next;

        // 在已排序部分寻找插入位置
        while (next!= NULL && next->male_score > current->male_score)
		{
            prev = next;
            next = next->next;
        }

        // 根据pre的位置，判断插入的位置(头节点后面插入或者pre结点后面插入)
        if (prev == first)
		{
            current->next = first->next;
            first->next = current;
        } else
		{
            current->next = prev->next;
            prev->next = current;
        }
    }
}

//按女子组总分排序
void Female_allscore(School *first)
{
	// 已排序部分初始化为原链表中第一个节点开始（头节点除外）
	School *sorted = first->next;
	//断开后面的结点
	first->next = NULL;
	
	while(sorted != NULL)
	{
		//记录当前结点
		School *current = sorted;
		// 记录下个结点，为下一次循环提供条件。
		sorted = sorted->next;
		
		// 从头结点开始，从已排序的部分寻找插入位置
		School *prev = first;
		//记录已排序的部分的下一个位置
		School *next = first->next;
		
		//在已排序部分寻找插入位置
		while(next != NULL && current->female_score < next->female_score)
		{
			prev = next;
			next = next->next;
		}
		
		//根据pre的位置，判断插入的位置(头节点后面插入或者pre结点后面插入)
		if(prev == first)
		{
			current->next = first->next;
			first->next = current;
		}
		else
		{
			current->next = prev->next;
			prev->next = current;
		}
	}
}

//按团体总分排序 
void Allscore(School *first)
{
	//已排序部分初始化为原链表中第一个节点开始（头结点除外） 
	School *sorted = first->next;
	//断开后面的节点
	first->next = NULL;
	
	while(sorted != NULL)
	{
		//记录当前的结点 
		School *current = sorted;
		//记录下个结点，为下一次循环提供条件
		sorted = sorted->next;
		
		//从头结点开始， 从已排序的部分寻找插入位置
		School *prev = first;
		//记录已排序的部分的下一个位置
		School *next = first->next;
		
		//在已排序部分寻找插入位置
		while(next != NULL && (current->all_score < next->all_score))
		{
			prev = next;
			next = next->next;	
		}
		
		//根据pre的位置，判断插入的位置(头节点后面插入或者pre结点后面插入)
		if(prev == first)
		{
			current->next = first->next;
			first->next = current;
		}
		else
		{
			current->next = prev->next;
			prev->next = current;
		} 
	 } 
}

//清零分数
void Clear(School *first)
{
	School *p = first->next;
	
	while(p != NULL)
	{
		//将所有分数赋为0 
		p->all_score = 0;
		p->female_score = 0;
		p->male_score = 0;
		
		p = p->next;
	}	
}

//二级目录功能实现-录入 
void Input(School *first_school, Sport *first_sport)
{
	char choice[5];
	
	while(1)
	{
		Input_menu();
		printf("请输入您的选择：\n");
		scanf("%s", choice);
		system("cls");
		
		if(strcmp(choice, "1") == 0)	//录入学校信息
		{
			system("cls");
			//根据用户选择，决定是否继续录入
			char select[2] = "1";
			while(strcmp(select, "1") == 0)
			{
				Input_school(first_school);
				system("cls");
				printf("是否继续录入(1.是  2.否)，请输入您的选择：\n");
				scanf("%s", select);
				system("cls");
			}	
		}
		else if(strcmp(choice, "2") == 0)	//录入体育项目信息
		{
			system("cls");
			char select[2] = "1";
			while(strcmp(select, "1") == 0)
			{
				Input_sport(first_sport);
				system("cls");
				printf("是否继续录入(1.是  2.否)，请输入您的选择：\n");
				scanf("%s", select);
				system("cls");
			}
		}
		else if(strcmp(choice, "3") == 0)	//录入体育项目比赛获奖信息
		{
			system("cls");
			char select[2] = "1";
			while(strcmp(select, "1") == 0)
			{
				Input_rank(first_sport, first_school);
				system("cls");
				printf("是否继续录入(1.是  2.否)，请输入您的选择：\n");
				scanf("%s", select);
				system("cls");
			}
		} 
		else if(strcmp(choice, "4") == 0)	//返回主菜单 
		{
			break;
		} 
		else	//非法输入 
		{
			printf("输入有误，请重新输入。\n");
			system("pause");
			system("cls");
		}   
	}
}

//二级目录功能实现-查询 
void Check(School *first_school, Sport *first_sport)
{
	char choice[5];
	
	while(1)
	{
		Check_menu();
		printf("请输入您的选择：\n");
		scanf("%s", choice);
		system("cls");
		
		if(strcmp(choice, "1") == 0)	//查询学校信息
		{
			Clear(first_school);
			Score(first_school, first_sport);
			int flag = 0;
			while(flag == 0)
			{
				flag = Get_school(first_school);
				system("pause");
				system("cls");	
			}
		}
		else if(strcmp(choice, "2") == 0)	//查询所有体育项目信息
		{
			Get_sport(first_sport);
			system("pause");
			system("cls");
		}
		else if(strcmp(choice, "3") == 0)	//查询学校项目的情况
		{
			Get_SchoolProjectData(first_school, first_sport);
			system("pause");
			system("cls");
		} 
		else if(strcmp(choice, "4") == 0)	//查询体育项目的比赛结果
		{
			Get_SportsResult(first_sport);
			system("pause");
			system("cls");
		}
		else if(strcmp(choice, "5") == 0)	//查询排名信息
		{
			Clear(first_school);
			Score(first_school, first_sport);
			getRankInfo(first_school);
			system("cls");
		} 
		else if(strcmp(choice, "6") == 0)	//最终获奖情况
		{
			Clear(first_school);
			Score(first_school, first_sport);
			Get_AwardDetails(first_school);
			system("pause");
			system("cls");
		} 
		else if(strcmp(choice, "7") == 0)	//返回主菜单 
		{
			break;
		}  
		else	//非法输入 
		{
			printf("输入有误，请重新输入。\n");
			system("pause");
			system("cls");
		}   
	}
}

//二级目录功能实现-修改
void Review(School *first_school, Sport *first_sport)
{
	char choice[5];
	
	while(1)
	{
		Review_menu();
		printf("请输入您的选择：\n");
		scanf("%s", choice);
		system("cls");
		
		if(strcmp(choice, "1") == 0)	//修改学校信息
		{
			Revise_shcool(first_school);
		}
		else if(strcmp(choice, "2") == 0)	//修改体育项目信息
		{
			Revise_sport(first_sport);
		}
		else if(strcmp(choice, "3") == 0)	//返回主菜单 
		{
			break;
		} 
		else	//非法输入 
		{
			printf("输入有误，请重新输入。\n");
			system("pause");
			system("cls");
		}   
	}
}

//释放内存 
void Free(School *first_school, Sport *first_sport)
{
	School *p = first_school;
	Sport *q = first_sport;
	
	while(p != NULL)	//释放链表School的内存 
	{
		School *temp = p->next;
		free(p);
		p = temp;
	}
	
	while(q != NULL)	//释放链表Sport的内存 
	{
		Sport *temp = q->next;
		free(q);
		q = temp;
	}
}

